<?php
/**
 * @file 3gplex/includes/views.inc
 * 
 */
require_once('./'. drupal_get_path('module', 'drupalplex') .'/includes/module.inc');

class plex_views extends plex_module
{
	static function add_view_dir()
	{
		
	}

	static function add_view_file()
	{
		
	}

	const alpha_pager_top = 0;
	const alpha_pager_bottom = 1;
	const alpha_pager_both = 2;

	const filter_current_user = "***CURRENT_USER***";

	static function add_view(
		$name,
		$description,
		array $page = null,
		array $block = null,
		array $fields = array(),
		array $filters = array(),
		array $sortkeys = array(),
		array $arguments = array())
	{
		$view = views_create_view($name, $description, array(2));
		if (!is_null($page)) {
			views_view_add_page($view, $page['title'], "ert", "teaser", true, 21, "header", 1, false);
		}
		
		if (!is_null($block)) {
			views_view_add_block($view, $block['title'], "list", 12, true, true, '', 0);
			$view->block_use_page_footer = false;
			$view->block_footer = "test";
			$view->block_footer_format = 1;
			$view->block_use_page_empty = true;
		}
		
		foreach ($sortkeys as $sortkey) {
			views_view_add_sort($view, "", "node.type", "ASC", "");
		}

		foreach ($arguments as $argument) {
			views_view_add_argument($view, "nodetype", "4", "Da Title Title for type", "");
			$view->argument[0]['wildcard'] = '';
			$view->argument[0]['wildcard_substitution'] = '';
		}
	
		foreach ($fields as $field) {
			views_view_add_field($view, "node", "title", "Da Title Title", 1, 'ASC', 'views_handler_field_nodelink_with_mark');
			$view->field[0]['options'] = 'link';
			
			views_view_add_field($view, "node_counter", "totalcount", "Seen");
			
			views_view_add_filter($view, "", "node.status", "=", "1", "");
			views_view_add_filter($view, "", "node.currentuid", "=", self::filter_current_user, "");
			
			views_view_add_exposed_filter($view, "", "node.status", 1, 0, 0, 0);
		}
		// label should be 'Published'?
		$vid = _views_save_view($view);

		$res = db_query("select 1 from {views_alpha_pager} where vid=%d", $vid);
		if ($res !== false && db_num_rows($res) > 0) {
			db_query("update {views_alpha_pager} set placement=%d where vid=%d", self::alpha_pager_both, $vid);
		} else {
			db_query("insert into {views_alpha_pager} set vid=%d, placement=%d", $vid, self::alpha_pager_both);
		}
	}

	static function rem_view_dir()
	{
		
	}

	static function rem_view_file()
	{

	}

	static function rem_view()
	{

	}
}

/**
 * @page plex_views_page Views
 */
